package com.cennavi.list;

import java.io.IOException;
import java.util.NavigableMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

import com.cennavi.dbs.HBasePool;

public class PbfVis extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8372850403285554461L;

	/**
	 * Constructor of the object.
	 */
	public PbfVis() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/x-protobuf");

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods",
				"POST, GET, OPTIONS, DELETE,PUT");


		String url = request.getRequestURI();

		String[] splits = url.split("/");

		int len = splits.length;

		int y = Integer.parseInt(splits[len - 1]);

		int x = Integer.parseInt(splits[len - 2]);

		int z = Integer.parseInt(splits[len - 3]);

		String tabName = splits[len - 4];

		Table tab = HBasePool.conn.getTable(TableName.valueOf(tabName));

		Get get = new Get((z + "," + x + "," + y).getBytes());

		Result result = tab.get(get);

		if (!result.isEmpty()) {

			NavigableMap<byte[], byte[]> map = result.getFamilyMap("data".getBytes());
			byte[] data = (byte[]) map.values().toArray()[0];
			response.getOutputStream().write(data);

		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
